#include "listformitem.h"
#include "qregularexpression.h"
#include "qsqlerror.h"
#include "qsqlquery.h"
#include <QUuid>
#include <QMediaPlayer>
#include <QMediaMetaData>
#include <QCoreApplication>

listFormitem::listFormitem() {}

listFormitem::listFormitem(QUrl url)
    :isLike(false)
    ,isHistory(false)
    ,itemurl(url)
{
    //QUuid
    itemid = QUuid::createUuid().toString();

    praseMusicitem(url);
}

void listFormitem::praseMusicitem(QUrl url)
{
    //创建播放源
    QMediaPlayer player;
    player.setMedia(url);
    //设置加载后返回源数据
    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }
    if(player.isMetaDataAvailable())
    {
        name = player.metaData("Title").toString();
        author = player.metaData("Author").toString();
        duration = player.metaData("Duration").toLongLong();
        album = player.metaData("AlbumTitle").toString();
        //处理未找到的值
        QString file = url.fileName();
        QRegularExpression rep("[～-]");
        int index = file.indexOf(rep);
        if(name.isEmpty())
        {
            if(index != -1)
            {
                name = file.mid(0,index);
            }
            else
            {
                name = file.mid(0,file.indexOf("."));
            }
        }
        if(author.isEmpty())
        {
            if(index != -1)
            {
                author = file.mid(index + 1,file.indexOf(".") - (index + 1));
            }
            else
            {
                author = "群星";
            }
        }
        if(album.isEmpty())
        {
            album = "未知专辑";
        }
        author = author.chopped(2);
    }
    player.play();
}

void listFormitem::insertMusicFromE()
{
    QSqlQuery query;
    query.prepare("select exists (select 1 from musiclist where itemid = ?)");
    query.addBindValue(itemid);
    if(!query.exec())
    {
        qDebug() << "查询失败" << query.lastError().text();
        return;
    }
    if(query.next())
    {
        bool isExists = query.value(0).toInt();
        if(isExists)
        {
            //存在，更新isLike,isHistory
            query.prepare("update musiclist set isLike = ?, isHistory = ? where itemid = ?");
            query.addBindValue(isLike ? 1:0);
            query.addBindValue(isHistory ? 1:0);
            query.addBindValue(itemid);
            query.exec();
            if(!query.exec())
            {
                qDebug() << "更新失败" << query.lastError().text();
                return;
            }
            qDebug() << "更新完成" << name;
        }
        else
        {
            //不存在，插入属性
            query.prepare("insert into musiclist(itemid,name,author,album,duration,itemurl,isLike,isHistory)\
                             values(?,?,?,?,?,?,?,?)");
            query.addBindValue(itemid);
            query.addBindValue(name);
            query.addBindValue(author);
            query.addBindValue(album);
            query.addBindValue(duration);
            query.addBindValue(itemurl.toLocalFile());
            query.addBindValue(isLike ? 1:0);
            query.addBindValue(isHistory ? 1:0);
            if(!query.exec())
            {
                qDebug() << "插入失败" << query.lastError().text();
            return;
            }
        }
    }
}

void listFormitem::setitemId(const QString &itemid)
{
    this->itemid = itemid;
}

void listFormitem::setNameitem(const QString& name)
{
    this->name = name;
}

void listFormitem::setAuthoritem(const QString& author)
{
    this->author = author;
}

void listFormitem::setAlbumitem(const QString& album)
{
    this->album = album;
}

void listFormitem::setDurationitem(const qint64 &duration)
{
    this->duration = duration;
}

void listFormitem::setisLikeitem(bool isLike)
{
    this->isLike = isLike;
}

void listFormitem::setisHistoryitem(bool isHistory)
{
    this->isHistory = isHistory;
}

void listFormitem::setUrlitem(const QUrl& itemurl)
{
    this->itemurl = itemurl;
}

QString listFormitem::getitemId() const
{
    return itemid;
}

QString listFormitem::getNameitem() const
{
    return name;
}

QString listFormitem::getAuthoritem() const
{
    return author;
}

QString listFormitem::getAlbumitem() const
{
    return album;
}

qint64 listFormitem::getDurationitem() const
{
    return duration;
}

bool listFormitem::getisLikeitem() const
{
    return isLike;
}

bool listFormitem::getisHistoryitem() const
{
    return isHistory;
}

QUrl listFormitem::getUrlitem() const
{
    return itemurl;
}

QString listFormitem::getLrcPath()
{
    QString path = itemurl.toLocalFile();
    path.replace(".mp3", ".lrc");
    path.replace(".flac",".lrc");
    path.replace(".ogg",".lrc");
    return path;
}
